<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: pi_clickandbuy_details.php 90 2013-09-21 12:04:27Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 90 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:04:27 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright © since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	© 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	© 2002-2003 osCommerce - www.oscommerce.com
* 	© 2003     nextcommerce - www.nextcommerce.org
* 	© 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * @category  PayIntelligent
 * @package   PayIntelligent_ClickandBuy
 * @copyright (C) 2010 PayIntelligent GmbH  <http://www.payintelligent.de/>
 * @license   GPLv2
 */

require_once('includes/application_top.php');
include('../lang/'.$_SESSION['language'].'/admin/pi_clickandbuy.php');

require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.clickandbuy.php');
$cab = new clickandbuy();

!empty($_GET['oID']) ? $shopOrderID = $_GET['oID'] : $shopOrderID = $_GET['oID'] ;

$authentication = $cab->getCabSettings('clickandbuy');

$cabOrderDetails = $db->db_query("SELECT * FROM ".DB_PREFIX."picab_orders WHERE shopOrderID ='".$shopOrderID."'");

$statusType = 'transactionID';		
$idList['transactionID1'] = $cabOrderDetails->fields['transactionID'];

$requestResult = $cab->statusRequest($authentication, $statusType, $idList);

$requestValues = $requestResult['values'];

if($requestResult['success'] != 1){
	$statusRequestResult['result'] = 'ERROR';
	$errorDescription = $requestResult['values']['detail']['errorDetails']['description'];

} else {
	$statusRequestResult['result'] = 'SUCCESS';
	$statusRequestResult['transactionID'] = $requestValues['transactionList']['transaction']['transactionID'];
	$statusRequestResult['externalID'] = $externalID;
	$statusRequestResult['transactionStatus'] = $requestValues['transactionList']['transaction']['transactionStatus'];
	$statusRequestResult['transactionType'] = $requestValues['transactionList']['transaction']['transactionType'];						
	$statusRequestResult['recurringPaymentAuthorizationID'] = $requestValues['transactionList']['transaction']['createdRecurringPaymentAuthorization']['recurringPaymentAuthorizationID'];						
	$statusRequestResult['recurringPaymentAuthorizationStatus'] = $requestValues['transactionList']['transaction']['createdRecurringPaymentAuthorization']['recurringPaymentAuthorizationStatus'];						
	$statusRequestResult['errorDetailCode'] = $requestValues['transactionList']['transaction']['errorDetails']['detailCode'];						
	$statusRequestResult['errorDescription'] = $requestValues['transactionList']['transaction']['errorDetails']['description'];
}	  		

$transactionList = getTransactionList();
$latestRecurringList = getLatestRecurringList();
$recurringTotalAmount = getrecurringTotalAmount();
if (empty($recurringTotalAmount)) 
	$recurringTotalAmount = '0.00';

function getTransactionList () {	
	global $shopOrderID, $db;
	$transactionList = array();
		
	$row = $db->db_query("SELECT *, DATE_FORMAT(created, '%d.%m.%Y %H:%m') AS created FROM ".DB_PREFIX."picab_transactions WHERE shopOrderID = '".$shopOrderID."' ORDER BY created ASC ");

	while (!$row->EOF) {
		$transactionList[] = $row->fields;
		$row->MoveNext();
	}
	return $transactionList;
}

function getLatestRecurringList () {	
	global $shopOrderID, $db;
	$latestRecurringList = array();
		
	$row = $db->db_query("SELECT *, DATE_FORMAT(created, '%d.%m.%Y %H:%m') AS created FROM ".DB_PREFIX."picab_transactions WHERE shopOrderID = '".$shopOrderID."' AND transactionType = 'recurring' ORDER BY created ASC LIMIT 0,5");

	while (!$row->EOF) {
		$latestRecurringList[] = $row->fields;
		$row->MoveNext();
	}
	return $latestRecurringList;
}

function getrecurringTotalAmount () {	
	global $shopOrderID, $db;
	$recurringTransactions = $db->db_query("SELECT SUM(amount) as recurringTotalAmount FROM ".DB_PREFIX."picab_transactions WHERE shopOrderID = '".$shopOrderID."' AND  transactionType = 'recurring'");
	$recurringTotalAmount = $recurringTransactions->fields['recurringTotalAmount'];	
	if (empty($recurringTotalAmount)) 
		$recurringTotalAmount = '0.00';
	
	return $recurringTotalAmount;
}

?>
<td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr>
	<td>
		<table border="0" width="100%" cellspacing="0" cellpadding="2" height="40">
			<tr>
				<td class="pageHeading"><?php echo CLICKANDBUY_ORDER_CLICKANDBUY_DETAILS; ?></td>
			</tr>
		</table>
	</td>
</tr>
<tr>
	<td>
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
			<tr>
				<td valign="top">
					<table cellspacing="0" cellpadding="0" border="0" width="98%">
						<tr>
							<td width="50%" valign="top">
								<table cellspacing="0" cellpadding="5" border="0" width="400">
									<tr class="infoBoxHeading">
										<td valign="top" class="infoBoxHeading" colspan="2">
											<b><?php echo CLICKANDBUY_ORDER_DETAILS_OVERVIEW; ?></b>
										</td>                      
									</tr>
									<tr>
										<td class="infoBoxContent" height="20" width="100"><?php echo CLICKANDBUY_ORDER_DETAILS_TOTAL_AMOUNT; ?>:</td>
										<td class="infoBoxContent"><?php echo $cabOrderDetails->fields['amount'].' '.$cabOrderDetails->fields['currency']; ?>  </td>
									</tr>
									<tr>
										<td class="infoBoxContent" height="20" width="100"><?php echo CLICKANDBUY_ORDER_DETAILS_DEBITED; ?>:</td>
										<td class="infoBoxContent"><?php echo $cabOrderDetails->fields['debited'].' '.$cabOrderDetails->fields['currency']; ?></td>
									</tr>
									<tr>
										<td class="infoBoxContent" height="20"><?php echo CLICKANDBUY_ORDER_DETAILS_REFUNDED; ?>:</td>
										<td class="infoBoxContent"><?php echo $cabOrderDetails->fields['refunded'].' '.$cabOrderDetails->fields['currency']; ?></td>
									</tr>
									<tr>
										<td class="infoBoxContent" height="20"><?php echo CLICKANDBUY_ORDER_DETAILS_CANCELLED; ?>:</td>
										<td class="infoBoxContent"><?php echo $cabOrderDetails->fields['cancelled'].' '.$cabOrderDetails->fields['currency']; ?></td>
									</tr>
								</table><br />
								<table cellspacing="0" cellpadding="5" border="0" width="400">
									<tr class="infoBoxHeading">
										<td valign="top" class="infoBoxHeading"><b><?php echo CLICKANDBUY_ORDER_DETAILS_REFUND; ?></b></td>                      
									</tr>
								</table>
									
								<table width="400" border="0" cellspacing="0" cellpadding="5" border="0">			
									<?php 											
									foreach ($transactionList as $item => $value) {										 	
										if ($value['transactionType'] == 'refund') {												
											$transactionTypeRefund = true;

											echo '
												<tr>
													<td class="infoBoxContent" width="140">'.$value['created'].'</td>
													<td class="infoBoxContent" width="100">'.$value['amount'].' '.$value['currency'].'</td>
													<td class="infoBoxContent" width="145">'.$value['status'].'</td>
													<td class="infoBoxContent" width="15">						          	
														<div class="infoBoxRight" style="position;relative">
															<a href="#">
															<img src="images/icon_info.gif" border="0" alt="" title="" />
															<span>
																<b>'.CLICKANDBUY_ORDER_DETAILS_REFUND.' '.CLICKANDBUY_ORDER_DETAILS.'</b><br />
																'.CLICKANDBUY_ORDER_DETAILS_DATE_TIME.': '.$value['created'].'<br />
																'.CLICKANDBUY_ORDER_DETAILS_EXTERNALID.': '.$value['externalID'].'<br />
																'.CLICKANDBUY_ORDER_DETAILS_STATUS.': '.$value['status'].'<br />
																'.CLICKANDBUY_ORDER_DETAILS_AMOUNT.': '.$value['amount'].' '.$value['currency'].'<br />
																'.CLICKANDBUY_ORDER_DETAILS_DESCRIPTION.': '.$value['description'].'<br />
															</span>
															</a>
														</div>
													</td>
												</tr>';																									
										}
									}
									if ($transactionTypeRefund) {
										echo '<tr>						
											<td class="infoBoxContent" colspan="4"><hr /></td>
											</tr>';
									}
									?>				
									<tr>
										<td class="infoBoxContent" height="20" colspan="4"><?php echo CLICKANDBUY_ORDER_DETAILS_REFUND_DESC; ?></td>
									</tr>
									<tr>
										<td class="infoBoxContent" height="20" colspan="4">
											<a class="button cab_button" href="pi_clickandbuy_refund.php?oID=<?php echo $shopOrderID; ?>">
												<?php echo CLICKANDBUY_ORDER_DETAILS_REFUND; ?>
											</a>
										</td>
									</tr>
								</table>		
								<br />
								
									<input type="hidden" name="oID" value="<?php echo $shopOrderID; ?>">
									<table cellspacing="0" cellpadding="5" border="0" width="400">
										<tr class="infoBoxHeading">
											<td valign="top" class="infoBoxHeading"><b><?php echo CLICKANDBUY_ORDER_DETAILS_CANCELLATION; ?></b></td>                      
										</tr>
									</table>
									<table width="400" border="0" cellspacing="0" cellpadding="5" border="0">			
										<?php 											
											foreach ($transactionList as $item => $value) {										 	
												if ($value['transactionType'] == 'cancel') {												
													$transactionTypeCancel = true;
													echo '	
														<tr>
															<td class="infoBoxContent" width="140">'.$value['created'].'</td>
															<td class="infoBoxContent" width="100">'.$value['amount'].' '.$value['currency'].'</td>
															<td class="infoBoxContent" width="145">'.$value['status'].'</td>
															<td class="infoBoxContent" width="15">
																<div class="infoBoxRight" style="position;relative">
																	<a href="#">
																		<img src="images/icon_info.gif" border="0" alt="" title="" />
																		<span>
																			<b>'.CLICKANDBUY_ORDER_DETAILS_CANCELLATION.' '.CLICKANDBUY_ORDER_DETAILS.'</b><br />
																			'.CLICKANDBUY_ORDER_DETAILS_DATE_TIME.': '.$value['created'].'<br />
																			'.CLICKANDBUY_ORDER_DETAILS_STATUS.': '.$value['status'].'<br />
																			'.CLICKANDBUY_ORDER_DETAILS_AMOUNT.': '.$value['amount'].' '.$value['currency'].'<br />
																		</span>
																	</a>
																</div>
															</td>
														</tr>';
												}
											}
											if ($transactionTypeCancel) {
												echo '<tr>						
													<td class="infoBoxContent" colspan="4"><hr /></td>
													</tr>';
											}
											?>													
											<tr>
												<td class="infoBoxContent" height="20" colspan="4">&nbsp;<?php echo CLICKANDBUY_ORDER_DETAILS_CANCELLATION_DESC; ?></td>
											</tr>
											<tr>
												<td class="infoBoxContent" height="20" colspan="4">
													<a class="button cab_button" href="pi_clickandbuy_cancel.php?oID=<?php echo $shopOrderID; ?>">
														<?php echo CLICKANDBUY_ORDER_DETAILS_CANCEL; ?>
													</a>
												</td>
											</tr>
										</table>		
									<br />
									
									<table cellspacing="0" cellpadding="5" border="0" width="400">
										<tbody>	
											<tr class="infoBoxHeading">
												<td valign="top" class="infoBoxHeading"><b><?php echo CLICKANDBUY_ORDER_DETAILS_CREDIT; ?></b></td>                      
											</tr>
										</tbody>
									</table>
									<table width="400" border="0" cellspacing="0" cellpadding="5" border="0">			
										<tbody>	
										<?php 											
										foreach ($transactionList as $item => $value) {										 	
											if ($value['transactionType'] == 'credit') {												
												$transactionTypeCredit = true;
												echo '	
													<tr>
													<td class="infoBoxContent" width="140">'.$value['created'].'</td>
													<td class="infoBoxContent" width="100">'.$value['amount'].' '.$value['currency'].'</td>
													<td class="infoBoxContent" width="145">'.$value['status'].'</td>
													<td class="infoBoxContent" width="15">
													<div class="infoBoxRight" style="position:relative;">
														<a href="#">
															<img src="images/icon_info.gif" border="0" alt="" title="" />
															<span>
																<b>'.CLICKANDBUY_ORDER_DETAILS_CREDIT.' '.CLICKANDBUY_ORDER_DETAILS.'</b><br />
																'.CLICKANDBUY_ORDER_DETAILS_DATE_TIME.': '.$value['created'].'<br />
																'.CLICKANDBUY_ORDER_DETAILS_EXTERNALID.': '.$value['externalID'].'<br />
																'.CLICKANDBUY_ORDER_DETAILS_STATUS.': '.$value['status'].'<br />
																'.CLICKANDBUY_ORDER_DETAILS_AMOUNT.': '.$value['amount'].' '.$value['currency'].'<br />
																'.CLICKANDBUY_ORDER_DETAILS_DESCRIPTION.': '.$value['description'].'
															</span>
														</a>
													</div>				
													</td>
													</tr>';
											}
										}
										if ($transactionTypeCancel) {
											echo '<tr>						
													<td class="infoBoxContent" colspan="4"><hr /></td>
												</tr>';
										}
										?>													
										<tr>
											<td class="infoBoxContent" height="20" colspan="4"><?php echo CLICKANDBUY_ORDER_DETAILS_CREDIT_DESC; ?></td>
										</tr>
										<tr>
											<td class="infoBoxContent" height="20" colspan="4">
												<a class="button cab_button" href="pi_clickandbuy_credit.php?oID=<?php echo $shopOrderID; ?>">
													<?php echo CLICKANDBUY_ORDER_DETAILS_CREDIT; ?>
												</a>
											</td>
										</tr>
									</table>
								</td>
								<td width="50%" valign="top">
									<table cellspacing="0" cellpadding="5" border="0" width="400">
										<tbody>	
											<tr class="infoBoxHeading">
												<td valign="top" class="infoBoxHeading"><b><?php echo CLICKANDBUY_ORDER_DETAILS_STATUS; ?>: </b></td>                      
											</tr>
										</tbody>
									</table>
									<table width="400" cellspacing="0" cellpadding="5" border="0">			
										<tbody>	
										<?php 
									if ($statusRequestResult['result'] == 'SUCCESS') {											
										if (!empty($statusRequestResult['transactionStatus'])) {
											echo '
												<tr>
												<td class="infoBoxContent" height="20" width="140">'.CLICKANDBUY_ORDER_DETAILS_TRANSACTIONID.':</td>
												<td class="infoBoxContent">'.$statusRequestResult['transactionID'].'</td>
												</tr>
												<tr>
												<td class="infoBoxContent">'.CLICKANDBUY_ORDER_DETAILS_TRANSACTION_STATUS.':</td>
												<td class="infoBoxContent">'.$statusRequestResult['transactionStatus'].'</td>
												</tr>
												<tr>
												<td class="infoBoxContent">'.CLICKANDBUY_ORDER_DETAILS_TRANSACTION_TYPE.':</td>
												<td class="infoBoxContent">'.$statusRequestResult['transactionType'].'</td>
												</tr>';
											if (!empty($statusRequestResult['recurringPaymentAuthorizationID'])) {
												echo '													
													<tr>
													<td class="infoBoxContent">'.CLICKANDBUY_ORDER_DETAILS_AUTHORIZATIONID.':</td>
													<td class="infoBoxContent">'.$statusRequestResult['recurringPaymentAuthorizationID'].'</td>
													</tr>
													<tr>
													<td class="infoBoxContent">'.CLICKANDBUY_ORDER_DETAILS_AUTHORIZATION_STATUS.':</td>
													<td class="infoBoxContent">'.$statusRequestResult['recurringPaymentAuthorizationStatus'].'</td>
													</tr>';
											} 
										} else {
											echo '
												<tr>
												<td class="infoBoxContent" height="20" width="140">'.CLICKANDBUY_ORDER_DETAILS_TRANSACTIONID.':</td>
												<td class="infoBoxContent">'.$statusRequestResult['transactionID'].'</td>
												</tr>
												<tr>
												<td class="infoBoxContent">'.CLICKANDBUY_ORDER_DETAILS_ERROR_CODE.':</td>
												<td class="infoBoxContent">'.$statusRequestResult['errorDetailCode'].'</td>
												</tr>
												<tr>
												<td class="infoBoxContent">'.CLICKANDBUY_ORDER_DETAILS_ERROR_DESC.':</td>
												<td class="infoBoxContent">'.$statusRequestResult['errorDescription'].'</td>
												</tr>';
										}
										
									} else {
										echo '<tr>
											<td class="infoBoxContent" height="20" colspan="2">'.CLICKANDBUY_ORDER_DETAILS_ERROR_REASON.'<br />'.$statusRequestResult['errorDescription'].'</td>
											</tr>';
									}
									?>
								</table><br />
								<input type="hidden" name="oID" value="<?php echo $shopOrderID; ?>" />
								<table cellspacing="0" cellpadding="5" border="0" width="400" border="0">
									<tr class="infoBoxHeading">
										<td valign="top" class="infoBoxHeading"><b><?php echo CLICKANDBUY_ORDER_DETAILS_MMS; ?>: </b></td>                      
									</tr>
								</table>
								<table width="400" cellspacing="0" cellpadding="5" border="0">			
									<tr>
										<td class="infoBoxContent">												
											<a class="button cab_button" href="pi_clickandbuy_mms.php?oID=<?php echo $shopOrderID; ?>">
												<?php echo CLICKANDBUY_ORDER_DETAILS_MMS_SHOW; ?>
											</a>
										</td>
									</tr>
								</table>	
							<br />
							<?php 
							if (($cabOrderDetails->fields['paymentType'] == 'clickandbuyrecurring') || ($cabOrderDetails->fields['paymentType'] == 'clickandbuypartialdelivery') || ($cabOrderDetails->fields['paymentType'] == 'clickandbuyfastcheckout')) {									
								?>						
								<table cellspacing="0" cellpadding="5" border="0" width="400">
									<tr class="infoBoxHeading">
										<td valign="top" class="infoBoxHeading"><b><?php echo CLICKANDBUY_ORDER_DETAILS_RECURRING; ?>: </b></td>                      
									</tr>
								</table>
								<table width="400" border="0" cellspacing="0" cellpadding="5">			
									<tr>
										<td class="infoBoxContent" height="20"><?php echo CLICKANDBUY_ORDER_DETAILS_AUTHORIZATION_AMOUNT; ?>:</td>
										<td class="infoBoxContent" colspan="3"><?php echo $cabOrderDetails->fields['recAmount'].' '.$cabOrderDetails->fields['currency']; ?></td>
									</tr>
									<tr>
										<td class="infoBoxContent" height="20"><?php echo CLICKANDBUY_ORDER_DETAILS_TOTAL_AMOUNT; ?>:</td>
										<td class="infoBoxContent" colspan="3"><?php echo $recurringTotalAmount.' '.$cabOrderDetails->fields['currency']; ?></td>
									</tr>
									<tr>						
										<td class="infoBoxContent" colspan="4"><hr /></td>
									</tr>
									<?php 
								if (!empty($latestRecurringList)) {		
									foreach ($latestRecurringList as $item => $value) {										 	
										if ($value['transactionType'] == 'recurring') {												
											$transactionTypeRecurring = true;									          					
											echo '	
												<tr>
													<td class="infoBoxContent" width="140">'.$value['created'].'</td>
													<td class="infoBoxContent" width="100">'.$value['amount'].' '.$value['currency'].'</td>
													<td class="infoBoxContent" width="145">'.$value['status'].'</td>
													<td class="infoBoxContent" width="15">
														<div class="infoBoxLeft" style="position;relative">
															<a href="#">
																<img src="images/icon_info.gif" border="0" alt="" title="">
																<span>
																	<b>'.CLICKANDBUY_ORDER_DETAILS_RECURRING.' '.CLICKANDBUY_ORDER_DETAILS.'</b><br />			
																	'.CLICKANDBUY_ORDER_DETAILS_DATE_TIME.': '.$value['created'].'<br />
																	'.CLICKANDBUY_ORDER_DETAILS_EXTERNALID.': '.$value['externalID'].'<br />
																	'.CLICKANDBUY_ORDER_DETAILS_STATUS.': '.$value['status'].'<br />
																	'.CLICKANDBUY_ORDER_DETAILS_AMOUNT.': '.$value['amount'].' '.$value['currency'].'<br />
																	'.CLICKANDBUY_ORDER_DETAILS_DESCRIPTION.': '.$value['description'].'<br />
																</span>
															</a>
														</div>			
													</td>
												</tr>';
										}
									}			
								}

								if ($transactionTypeRecurring) {
									echo '<tr>						
											<td class="infoBoxContent" colspan="4"><hr /></td>
										</tr>';
								}												
								?> 
								<tr>						
									<td class="infoBoxContent" colspan="4"><?php echo CLICKANDBUY_ORDER_DETAILS_RECURRING_DESC; ?></td>
								</tr>
								<tr>
									<td class="infoBoxContent">
										<input class="button" type="submit" value="" name="recDebit" />
										<a class="button cab_button" href="pi_clickandbuy_recurring_debit.php?oID=<?php echo $shopOrderID; ?>">
											<?php echo CLICKANDBUY_ORDER_DETAILS_DEBIT_TRANSACTION; ?>
										</a>
									</td>
									<td class="infoBoxContent" colspan="3">
										<a class="button cab_button" href="pi_clickandbuy_recurring.php?oID=<?php echo $shopOrderID; ?>">
											<?php echo CLICKANDBUY_ORDER_DETAILS_SHOW_TRANSACTIONS; ?>
										</a>
									</td>
								</tr>
							</tbody>
						</table><br />	
						<?php  } ?>								
					</td>
				</tr>	
			</table>
		</td>
	</tr>
</table>